<%!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

%><%#service:functions%><%#function:returnType%><%^function:starts_interaction?%>

<%^function:eb%><%^service:interaction?%>
<% > service_common/function_return_type %> <%service:name%>SvIf::<%function:cpp_name%>(<% > service_common/function_return_param %><% > service_common/function_param_list_commented_out%>) {
  apache::thrift::detail::si::throw_app_exn_unimplemented("<%function:name%>");
}

folly::SemiFuture<<% > service_common/async_return_type %>> <%service:name%>SvIf::semifuture_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
<%^type:resolves_to_complex_return?%>
  return apache::thrift::detail::si::semifuture([&] {
    return <%function:cpp_name%>(<% > service_common/param_list_move%>);
  });
<%/type:resolves_to_complex_return?%>
<%#type:resolves_to_complex_return?%>
  return apache::thrift::detail::si::semifuture_returning<%^function:stack_arguments?%>_uptr<%/function:stack_arguments?%>([&](<% > types/type%>& _return) { <%function:cpp_name%>(_return<%function:comma%><% > service_common/param_list_move%>); });
<%/type:resolves_to_complex_return?%>
}

folly::Future<<% > service_common/async_return_type %>> <%service:name%>SvIf::future_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
  return apache::thrift::detail::si::future(semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>), getThreadManager());
}

<%#function:coroutine?%>
#if FOLLY_HAS_COROUTINES
folly::coro::Task<<% > service_common/callback_type %>> <%service:name%>SvIf::co_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
  return folly::coro::toTask(future_<%function:cpp_name%>(<% > service_common/param_list_move%>));
}

folly::coro::Task<<% > service_common/callback_type %>> <%service:name%>SvIf::co_<%function:cpp_name%>(apache::thrift::RequestParams /* params */<%function:comma%><% > service_common/function_param_list%>) {
  return co_<%function:cpp_name%>(<% > service_common/param_list_move%>);
}
#endif // FOLLY_HAS_COROUTINES
<%/function:coroutine?%>
<%/service:interaction?%>
<%#service:interaction?%>
#if FOLLY_HAS_COROUTINES
folly::coro::Task<<% > service_common/callback_type %>> <%service:parent_service_name%>SvIf::<%service:name%>If::co_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
  return folly::coro::toTask(semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>));
}

folly::coro::Task<<% > service_common/callback_type %>> <%service:parent_service_name%>SvIf::<%service:name%>If::co_<%function:cpp_name%>(apache::thrift::RequestParams /* params */<%function:comma%><% > service_common/function_param_list%>) {
  return co_<%function:cpp_name%>(<% > service_common/param_list_move%>);
}
#endif // FOLLY_HAS_COROUTINES
folly::SemiFuture<<% > service_common/async_return_type %>> <%service:parent_service_name%>SvIf::<%service:name%>If::semifuture_<%function:cpp_name%>(<% > service_common/function_param_list_commented_out%>) {
  apache::thrift::detail::si::throw_app_exn_unimplemented("semifuture_<%function:name%>");
}
<%/service:interaction?%><%/function:eb%>
<%#function:oneway?%>
<%^function:eb%>
<%^function:coroutine?%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallbackBase> callback<%function:comma%><% > service_common/function_param_list%>) {
  apache::thrift::detail::si::async_tm_oneway(this, std::move(callback), [&] {
    return future_<%function:cpp_name%>(<% > service_common/param_list_move%>);
  });
}
<%/function:coroutine?%>
<%#function:coroutine?%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallbackBase> callback<%function:comma%><% > service_common/function_param_list%>) {
#if FOLLY_HAS_COROUTINES
  // It's possible the coroutine versions will delegate to a future-based
  // version. If that happens, we need the RequestParams arguments to be
  // available to the future through the thread-local backchannel, so we have to
  // set them up now. This is just a short-term shim; in the long run, we
  // shouldn't generate the future, semifuture, and synchronous member functions
  // for entry points that explicitly ask for coroutines.
  apache::thrift::detail::si::async_tm_prep(this, callback.get());
  apache::thrift::RequestParams params{callback->getConnectionContext(),
    callback->getThreadManager(), callback->getEventBase()};
  try {
    apache::thrift::detail::si::async_tm_oneway_coro_start(
      co_<%function:cpp_name%>(params<%function:comma%><% > service_common/param_list_move%>),
      params.getThreadManager(),
      std::move(callback));
  } catch (...) {
    callback->exception(std::current_exception());
  }
#else // FOLLY_HAS_COROUTINES
<%^service:interaction?%>
  apache::thrift::detail::si::async_tm_oneway(this, std::move(callback), [&] {
    return future_<%function:cpp_name%>(<% > service_common/param_list_move%>);
  });
<%/service:interaction?%><%#service:interaction?%>
  apache::thrift::detail::si::async_tm_oneway(this, std::move(callback), [&] {
    return apache::thrift::detail::si::future(apache::thrift::detail::si::semifuture([&] {
      return semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>);
    }), getThreadManager());
  });
<%/service:interaction?%>
#endif // FOLLY_HAS_COROUTINES
}
<%/function:coroutine?%>
<%/function:eb%>
<%#function:eb%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_eb_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallbackBase> /*callback*/<%function:comma%><% > service_common/function_param_list_commented_out%>) {
  LOG(DFATAL) << "Function <%function:cpp_name%> is unimplemented";
}
<%/function:eb%>
<%/function:oneway?%>
<%^function:oneway?%>
<%^function:eb%>
<%^function:coroutine?%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallback<<% > types/unique_ptr_type%>>> callback<%function:comma%><% > service_common/function_param_list%>) {
  apache::thrift::detail::si::async_tm(this, std::move(callback), [&] {
    return future_<%function:cpp_name%>(<% > service_common/param_list_move%>);
  });
}
<%/function:coroutine?%>
<%#function:coroutine?%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallback<<% > types/unique_ptr_type%>>> callback<%function:comma%><% > service_common/function_param_list%>) {
#if FOLLY_HAS_COROUTINES
  // It's possible the coroutine versions will delegate to a future-based
  // version. If that happens, we need the RequestParams arguments to be
  // available to the future through the thread-local backchannel, so we have to
  // set them up now. This is just a short-term shim; in the long run, we
  // shouldn't generate the future, semifuture, and synchronous member functions
  // for entry points that explicitly ask for coroutines.
  apache::thrift::detail::si::async_tm_prep(this, callback.get());
  apache::thrift::RequestParams params{callback->getConnectionContext(),
    callback->getThreadManager(), callback->getEventBase()};
  try {
    apache::thrift::detail::si::async_tm_coro_start(
      co_<%function:cpp_name%>(params<%function:comma%><% > service_common/param_list_move%>),
      params.getThreadManager(),
      std::move(callback));
  } catch (...) {
    callback->exception(std::current_exception());
  }
#else // FOLLY_HAS_COROUTINES
<%^service:interaction?%>
  apache::thrift::detail::si::async_tm(this, std::move(callback), [&] {
    return future_<%function:cpp_name%>(<% > service_common/param_list_move%>);
  });
<%/service:interaction?%><%#service:interaction?%>
  apache::thrift::detail::si::async_tm(this, std::move(callback), [&] {
    return apache::thrift::detail::si::future(
      semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>), getThreadManager());
  });
<%/service:interaction?%>
#endif // FOLLY_HAS_COROUTINES
}
<%/function:coroutine?%>
<%/function:eb%>
<%#function:eb%>
void <%service:parent_service_name%>SvIf::<%#service:interaction?%><%service:name%>If::<%/service:interaction?%>async_eb_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallback<<% > types/unique_ptr_type%>>> callback<%function:comma%><% > service_common/function_param_list_commented_out%>) {
  callback->exception(apache::thrift::TApplicationException("Function <%function:cpp_name%> is unimplemented"));
}
<%/function:eb%>
<%/function:oneway?%>
<%/function:starts_interaction?%><%#function:starts_interaction?%>
std::unique_ptr<<%service:name%>SvIf::<%type:name%>If> <%service:name%>SvIf::<%function:cpp_name%>() {
  apache::thrift::detail::si::throw_app_exn_unimplemented("<%function:name%>");
}
<%/function:starts_interaction?%>
<%/function:returnType%><%/service:functions%>
